const app = getApp()
import { requestApi } from '../../../api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    channel: '',
  },
  // save
  formSubmit: function (e) {
    let nickname = e.detail.value.nickname
    let data = {
      nickname: nickname,
      channel: this.data.channel
    }
    let options = {
      url: requestApi.setNameUrl,
      data: data,
      method: 'POST'
    }
    let pages = getCurrentPages()  // 获取当前的页面栈信息
    
    // 调用网络请求
    app.request(options,
    (res)=> {
      // 当前页面data数据更新
      this.setData({
        'data.nickname': nickname
      })
      let prevPage = pages[pages.length - 2]
      prevPage.setData({
        'data.nickname': nickname
      })
      //  存储storage
      wx.setStorage({
        key: 'profileinfo',
        data: this.data.data ,
      })
      wx.navigateBack({})
    },
    (error) => {
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'

    this.setData({
      channel: channel,
    })

    wx.getStorage({
     key: 'profileinfo',
     success: (res)=> {
       this.setData({
         data: res.data   
       })
     },
   })
  },
})